package com.lw.common.customview

import android.content.Context
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import com.hjq.toast.Toaster
import com.lw.common.R
import com.lw.common.network.UrlUtils.setCustomUrl
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils

class CustomCenterPopup(context: Context) : CenterPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.popup_custom_center
    }

    override fun onCreate() {
        super.onCreate()
        val popClose = findViewById<AppCompatImageView>(R.id.pop_close)
        val popCancel = findViewById<AppCompatTextView>(R.id.pop_cancel)
        val popConfirm = findViewById<Button>(R.id.pop_confirm)
        val etNetwork = findViewById<IPEditText>(R.id.et_network)
        val etPort = findViewById<EditText>(R.id.et_port)

        popClose.setOnClickListener { dismiss() }
        popCancel.setOnClickListener { dismiss() }
        popConfirm.setOnClickListener {
            val etIp1 = etNetwork.findViewById<EditText>(R.id.edit1)
            val etIp2 = etNetwork.findViewById<EditText>(R.id.edit2)
            val etIp3 = etNetwork.findViewById<EditText>(R.id.edit3)
            val etIp4 = etNetwork.findViewById<EditText>(R.id.edit4)
            if (etIp1.text.toString().isEmpty()) {
                return@setOnClickListener
            }
            if (etIp2.text.toString().isEmpty()) {
                return@setOnClickListener
            }
            if (etIp3.text.toString().isEmpty()) {
                return@setOnClickListener
            }
            if (etIp4.text.toString().isEmpty()) {
                return@setOnClickListener
            }
            val ip =
                etIp1.text.toString() + "." + etIp2.text.toString() + "." + etIp3.text.toString() + "." + etIp4.text.toString()
            val port = etPort.text.toString()
            val url = "http://$ip:$port/wses18/v1/api/services/"
            setCustomUrl(url)
            Toaster.show("修改成功")
            dismiss()
        }
    }

    override fun getMaxHeight(): Int {
        return XPopupUtils.getScreenHeight(context) / 2
    }
}